package com.ruoyi.mobile.project.pms.service;

import com.github.pagehelper.PageHelper;
import com.ruoyi.db.pms.domain.PmsComment;
import com.ruoyi.db.pms.domain.query.PmsCommentQueryVo;
import com.ruoyi.db.pms.mapper.PmsCommentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class WxPmsCommentService
{
    @Autowired
    private PmsCommentMapper commentMapper;

    /**
     * 获取商品评论列表
     *
     * @author 张涛
     * @param id
     * @param offset
     * @param limit
     * @return
     */
    public List<PmsComment> queryGoodsByGid(Long id, int offset, int limit)
    {
        PmsCommentQueryVo pmsCommentQueryVo = new PmsCommentQueryVo();
        pmsCommentQueryVo.setValueId(id);
        pmsCommentQueryVo.setType(0);
        PageHelper.startPage(offset, limit);
        return commentMapper.selectPmsCommentList(pmsCommentQueryVo);
    }

    /**
     * 获取商品评论列表
     *
     * @author 张涛
     * @param type
     * @param valueId
     * @param showType
     * @param offset
     * @param limit
     * @return
     */
    public List<PmsComment> query(Integer type, Long valueId, Integer showType, Integer offset, Integer limit)
    {
        PmsCommentQueryVo pmsCommentQueryVo = new PmsCommentQueryVo();
        //如果showType=0则查询所有评论
        if (showType == 0) {
            pmsCommentQueryVo.setValueId(valueId);
            pmsCommentQueryVo.setType(type);
        }
        //如果showType=1则查询有图评论
        else if (showType == 1) {
            pmsCommentQueryVo.setValueId(valueId);
            pmsCommentQueryVo.setType(type);
            pmsCommentQueryVo.setHasPicture(true);
        }
        else {
            throw new RuntimeException("showType不支持");
        }
        PageHelper.startPage(offset, limit);
        return commentMapper.selectPmsCommentList(pmsCommentQueryVo);
    }

    /**
     * 获取商品评论数量
     *
     * @author 张涛
     * @param type
     * @param valueId
     * @param showType
     * @return
     */
    public int count(Integer type, Long valueId, Integer showType)
    {
        PmsCommentQueryVo pmsCommentQueryVo = new PmsCommentQueryVo();
        //如果showType=0则查询所有评论
        if (showType == 0) {
            pmsCommentQueryVo.setValueId(valueId);
            pmsCommentQueryVo.setType(type);
        }
        //如果showType=1则查询有图评论
        else if (showType == 1) {
            pmsCommentQueryVo.setValueId(valueId);
            pmsCommentQueryVo.setType(type);
            pmsCommentQueryVo.setHasPicture(true);
        }
        else {
            throw new RuntimeException("showType不支持");
        }
        return commentMapper.countComments(pmsCommentQueryVo);
    }

    /**
     * 新建评论
     *
     * @author 严方亚
     * @date 2020-05-17
     * @param comment
     */
    public int save(PmsComment comment)
    {
        comment.setAddTime(LocalDateTime.now());
        comment.setUpdateTime(LocalDateTime.now());
        comment.setAdminContent(" ");
        return commentMapper.insertPmsComment(comment);
    }

    public List<PmsComment> querySelective(String userId, String valueId, Integer page, Integer size, String sort, String order)
    {
        return null;
    }

    /**
     * 删除评论
     *
     * @author 张涛
     * @param id
     */
    public void deleteById(Long id)
    {
        commentMapper.deletePmsCommentById(id);
    }

    public PmsComment findById(Long id)
    {
        return null;
    }

    /**
     * 修改评论
     *
     * @author 张涛
     * @param comment
     * @return
     */
    public int updateById(PmsComment comment)
    {
        comment.setUpdateTime(LocalDateTime.now());
        return commentMapper.updatePmsComment(comment);
    }
}
